package com.xdf.docmanagementsystem.service.impl;

import com.xdf.docmanagementsystem.common.base.BaseService;
import com.xdf.docmanagementsystem.common.util.Example;
import com.xdf.docmanagementsystem.enums.ResponseEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xdf.docmanagementsystem.entity.UserDeploy;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.xdf.docmanagementsystem.dao.UserDeployMapper;
import com.xdf.docmanagementsystem.service.UserDeployService;
import org.springframework.util.Assert;

/**
 * @Description : com.xdf.docmanagementsystem.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : ZK
 * @Date : 2020-06-22 13:00:25
 */
@Service
public class UserDeployServiceImpl extends BaseService implements UserDeployService {
    @Autowired
    private UserDeployMapper dao;

    /**
     * @Describe 新增方法
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(UserDeploy record) throws Exception {
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(UserDeploy record) throws Exception {
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<UserDeploy> list) throws Exception {
        Integer userId = getUserId();
        list = Optional.ofNullable(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(x -> x.getMenuId() != null && x.getField() != null)
                .map(x -> {
                    x.setUserId(getUserId());
                    if (x.getHidden() == null) x.setHidden(false);
                    if (x.getWidth() != null) x.setWidth(100D);
                    return x;
                })
                .collect(Collectors.toList());
        return dao.multiInsert(list);
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(UserDeploy record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(UserDeploy record) throws Exception {
        Assert.notNull(record, ResponseEnum.NOT_ALLOW.getMessage());
        Assert.notNull(record.getField(), ResponseEnum.NOT_ALLOW.getMessage());
        Assert.notNull(record.getMenuId(), ResponseEnum.NOT_ALLOW.getMessage());
        Example example = new Example()
                .andEq("user_id", getUserId())
                .andEq("menu_id", record.getMenuId())
                .andEq("field", record.getField());
        List<UserDeploy> list = dao.selectByExample(example);
        if (list != null && list.size() > 0) {
            return dao.updateByExampleSelective(record, example);
        } else {
            record.setUserId(getUserId());
            return dao.insertSelective(record);
        }
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UserDeploy record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<UserDeploy> list) throws Exception {
        if (list == null || list.size() <= 0) return 0;
        int count = 0;
        for (UserDeploy userDeploy : list) {
            userDeploy.setUserId(getUserId());
            Example example = new Example()
                    .andEq("user_id", getUserId())
                    .andEq("menu_id", userDeploy.getMenuId())
                    .andEq("field", userDeploy.getField());
            List<UserDeploy> items = dao.selectByExample(example);
            if (items != null && items.size() > 0) {
                count += dao.updateByExampleSelective(userDeploy, example);
            } else {
                userDeploy.setAlign("center");
                userDeploy.setHidden(false);
                userDeploy.setSerialNo(1);
                count += dao.insertSelective(userDeploy);
            }
        }
        return count;
    }

    /**
     * @Describe 根据主键查询
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    public UserDeploy selectByPrimaryKey(Integer id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * @Describe 根据主键删除
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Integer id) throws Exception {
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author ZK
     * @Date 2020-06-22 13:00:25
     */
    @Override
    public List<UserDeploy> listQuery(UserDeploy record) throws Exception {
        return dao.selectByParam(record);
    }

    /**
     * 重置网格
     *
     * @param menuId
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int resetDeploy(Integer menuId) throws Exception {
        Assert.state(menuId != null, ResponseEnum.NOT_ALLOW.getMessage());
        UserDeploy userDeploy = UserDeploy
                .builder()
                .menuId(menuId)
                .userId(getUserId())
                .align("center")
                .headAlign("center")
                .hidden(false)
                .width(150D)
                .serialNo(1)
                .build();
        Example example = new Example().andEq("menu_id", menuId).andEq("user_id", getUserId());
        return dao.updateByExampleSelective(userDeploy, example);
    }

    /**
     * 根据菜单主键查询
     *
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UserDeploy> listQuery(List<UserDeploy> list) throws Exception {
        list = Optional.ofNullable(list).orElse(new ArrayList<>())
                .stream()
                .filter(x -> x.getMenuId() != null && !StringUtils.isEmpty(x.getField()))
                .map(x -> {
                    x.setUserId(getUserId());
                    return x;
                })
                .collect(Collectors.toList());
        if (list == null || list.size() <= 0) return list;

        Integer menuId = list.get(0).getMenuId();
        for (UserDeploy userDeploy : list) {
            Example example = new Example();
            example.andEq("menu_id", menuId);
            example.andEq("user_id", getUserId());
            example.andEq("field", userDeploy.getField());
            List<UserDeploy> items = dao.selectByExample(example);
            if (items == null || items.size() <= 0) {
                dao.insertSelective(UserDeploy
                        .builder()
                        .menuId(menuId)
                        .userId(getUserId())
                        .field(userDeploy.getField())
                        .width(userDeploy.getWidth())
                        .serialNo(userDeploy.getSerialNo())
                        .hidden(userDeploy.getHidden())
                        .build());
            }
        }
        List<UserDeploy> result = dao.selectByParam(UserDeploy.builder()
                .userId(getUserId())
                .menuId(menuId)
                .build());
        return result;
    }

}